package com.yzkj.cpjxc.business.controller;

import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.CSRFTokenManager;
import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.common.Pager;
import com.yzkj.cpjxc.business.system.interceptor.DoLog;
import com.yzkj.cpjxc.business.system.interceptor.Permission;
import com.yzkj.cpjxc.business.domain.Multimedia;
import com.yzkj.cpjxc.business.domain.MultimediaQueryVo;
import com.yzkj.cpjxc.business.service.MultimediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * 多媒体文件控制器
 */
@Controller
@RequestMapping("/multimedia")
public class MultimediaController extends BaseController {

	@Autowired
	private MultimediaService multimediaService;

	@RequestMapping("/list")
	@DoLog(cnContent = "查看多媒体文件列表信息")
	@Permission(name="multimedia.view")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, Multimedia multimedia) throws Exception {
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("multimedia", multimedia);
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("multimedia/list").addAllObjects(modelMap);
	}

	@RequestMapping("/ajaxPage")
	@DoLog(cnContent = "多媒体文件列表ajax分页")
	@Permission(name="multimedia.view")
	public String ajaxPage(HttpServletRequest request, HttpServletResponse response, MultimediaQueryVo queryVo) throws Exception {
		try {
			Pager<Multimedia> p = new Pager<Multimedia>();
			String currentPage = request.getParameter(Pager.MY_PAGINATION_CURRENT_PAGE);
			p.setCurrentPage(Integer.parseInt(currentPage == null ? "1" : currentPage));
			Pager<Multimedia> pager = multimediaService.search(p, queryVo);
			this.genPageJson(response, pager);
			return null;
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/toAdd")
	@DoLog(cnContent = "去到添加多媒体文件页面")
	@Permission(name="multimedia.add")
	public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response, Multimedia multimedia) throws Exception {
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("multimedia/add").addAllObjects(modelMap);
	}

	@RequestMapping("/add")
	@DoLog(cnContent = "添加多媒体文件")
	@Permission(name="multimedia.add")
	public String add(HttpServletRequest request, HttpServletResponse response, Multimedia multimedia) throws Exception {
		try {
			BusinessMap bm = multimediaService.create(multimedia);
			return this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/toEdit")
	@DoLog(cnContent = "去到编辑多媒体文件页面")
	@Permission(name="multimedia.edit")
	public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response, Multimedia multimedia) throws Exception {
		Multimedia result = multimediaService.getById(multimedia.getUuid());
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		modelMap.put("multimedia", result);
		return new ModelAndView("multimedia/edit").addAllObjects(modelMap);
	}

	@RequestMapping("/edit")
	@DoLog(cnContent = "编辑多媒体文件")
	@Permission(name="multimedia.edit")
	public String edit(HttpServletRequest request, HttpServletResponse response, Multimedia multimedia) throws Exception {
		try {
			BusinessMap bm = multimediaService.edit(multimedia);
			return this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/view")
	@DoLog(cnContent = "查看多媒体文件信息")
	@Permission(name="multimedia.view")
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response, Multimedia multimedia) throws Exception {
		Multimedia result = multimediaService.getById(multimedia.getUuid());
		return new ModelAndView("multimedia/view").addObject("multimedia", result);
	}

	@RequestMapping("/delete")
	@DoLog(cnContent = "逻辑删除多媒体文件信息")
	@Permission(name="multimedia.delete")
	public String delete(HttpServletRequest request, HttpServletResponse response, Multimedia multimedia) throws Exception {
		try {
			boolean flag = multimediaService.falseDelete(multimedia.getUuid());
			return this.ajax(response, flag ? Constants.SUCCESS : Constants.ERROR);
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	/**
	 * 根据ID 提供文件下载
	 */
	@RequestMapping("/download")
	@DoLog(cnContent = "Multimedia提供文件下载")
	public String downloadApplyFrom(HttpServletRequest request, HttpServletResponse response, Multimedia multimedia) throws Exception {
		if (multimedia != null) {
			Multimedia file = multimediaService.getById(multimedia.getUuid());
			byte[] fileByte = file.getFileData();
			//设置相应头信息
			String infoName = file.getOidFileName();
			if (infoName == null) {
				infoName = "文件";
			}
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(infoName.getBytes("GBK"), "iso-8859-1"));
			OutputStream outputSream = null;
			InputStream in = null;
			try {
				outputSream = response.getOutputStream();
				in = new ByteArrayInputStream(fileByte);
				int len = 0;
				byte[] buf = new byte[1024];
				while ((len = in.read(buf, 0, 1024)) != -1) {
					outputSream.write(buf, 0, len);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (in != null) {
						in.close();
					}
					if (outputSream != null) {
						outputSream.flush();
						outputSream.close();
					}

				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}
		return null;
	}

}
